package com.hsj.goldstore.mvp.view.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hsj.goldstore.R;
import com.hsj.goldstore.adapter.GoodsAdapter;
import com.hsj.goldstore.adapter.GridItemDivider;
import com.hsj.goldstore.base.BaseMvpFrament;
import com.hsj.goldstore.base.PresenterFactory;
import com.hsj.goldstore.base.PresenterLoader;
import com.hsj.goldstore.bean.GoodsBean;
import com.hsj.goldstore.bean.LoginBean;
import com.hsj.goldstore.mvp.present.fragment.GoodsTypePresent;
import com.hsj.goldstore.utils.Helper;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/26.
 */

@SuppressLint("ValidFragment")
public class GoodsTypeFagment extends BaseMvpFrament<GoodsTypePresent, GoodsTypeView> implements GoodsTypeView, XRecyclerView.LoadingListener ,GoodsAdapter.MyItemClick{
    @BindView(R.id.m_recyclerview)
    XRecyclerView mRecyclerview;
    Unbinder unbinder;
    private GoodsAdapter adapter;
    //type :0 全部商品 1 黄金 2 铂金
    private int type;

    public GoodsTypeFagment(int type) {
        this.type = type;
    }

    @Override
    protected int getResouseId() {

        return R.layout.fragment_goods_type;
    }

    @Override
    protected void initView() {
        initRecyclyview();
    }

    private void initRecyclyview() {


        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        mRecyclerview.setLayoutManager(manager);
//        mRecyclerview.setPullRefreshEnabled(true);
//        mRecyclerview.setLoadingMoreEnabled(true);
//        mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
//        mRecyclerview.setLoadingListener(this);
        mRecyclerview.addItemDecoration(new GridItemDivider());
    }

    @Override
    protected void initData() {
        presenter.initData();
    }

    @Override
    public Loader<GoodsTypePresent> onCreateLoader(int id, Bundle args) {
        return new PresenterLoader<>(getActivity(), new PresenterFactory<GoodsTypePresent>() {
            @Override
            public GoodsTypePresent create() {
                return new GoodsTypePresent();
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        presenter.refresh();
    }

    @Override
    public void onLoadMore() {
        presenter.loadMore();
    }

    @Override
    public void setData(List<GoodsBean> list) {
        adapter = new GoodsAdapter(getActivity(),list);
        adapter.onMyItemClick(this);
        mRecyclerview.setAdapter(adapter);
    }

    @Override
    public void refreshComplete() {
        mRecyclerview.refreshComplete();
    }

    @Override
    public void noMore(boolean more) {
        mRecyclerview.setNoMore(more);
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public void headClick(int position) {
        Helper.showToast(getActivity(), "点击爱心");
    }
}
